// pages/search/search.js
const db = wx.cloud.database();
const _ = db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchKey: '',
    result: [],
    timer: []
  },

  //---------------------------------------------Methods START---------------------------------------------------------
  inputSearch(e) {
    //频繁输入先清除之前的定时器
    this.setData({
      timer: []
    });
    let that = this;
    this.setData({
      searchKey: e.detail.value
    })
    let tmpTimer = setTimeout(function () {
      that.handleSearch();
      let newTimer = that.data.timer;
      newTimer.push(tmpTimer);
      that.setData({
        timer: newTimer
      })
    }, 500);
  },
  handleSearch() {
    let that = this;
    wx.showLoading({
      title: '搜索中',
    })
    if (that.data.searchKey == "") {
      that.setData({
        result: []
      });
      console.log('关键字为空');
      wx.hideLoading();
      return
    }
    //根据工作名查
    let tmpRegexp = '';
    for (let i = 0; i < that.data.searchKey.length; i++) {
      tmpRegexp = tmpRegexp + '.*' + that.data.searchKey[i]
    }
    console.log(tmpRegexp);
    db.collection('job').where({
      jobName: db.RegExp({
        regexp: tmpRegexp,
        //大小写不区分
        options: 'i'
      })
    }).get({
      success: res1 => {
        // console.log("res1.data:");
        // console.log(res1.data);
        //根据村名查
        db.collection('job').where({
          village: db.RegExp({
            regexp: tmpRegexp,
            //大小写不区分
            options: 'i'
          })
        }).get({
          success: res2 => {
            // console.log("res2.data:")
            // console.log(res2.data);
            //数组去重
            let realResult;
            let longerOne;
            let shorterOne;
            //浅拷贝即可
            if (res1.data.length >= res2.data.length) {
              longerOne = res1.data;
              shorterOne = res2.data;
            } else {
              longerOne = res2.data;
              shorterOne = res1.data;
            }
            realResult = JSON.parse(JSON.stringify(longerOne));
            for (let i = 0; i < shorterOne.length; i++) {
              for (let j = 0; j < longerOne.length; j++) {
                if (shorterOne[i]._id != longerOne[j]._id) {
                  realResult.push(shorterOne[i]);
                  // console.log("发现不一样的_id:" + shorterOne[i]._id) + ",加入结果";
                }
              }
            }
            that.setData({
              result: realResult
            });
            // console.log("result:");
            // console.log(that.data.result);
          }
        });
      }
    });
    wx.hideLoading();
  },


  navToDetail(e) {
    console.log(e.currentTarget.dataset);
    let i = e.currentTarget.dataset.i;
    let conveyData;
    conveyData = this.data.result[i];
    console.log(conveyData);
    getApp().navTmpInfo = conveyData;
    wx.navigateTo({
      url: '/pages/job_detail/job_detail',
    })
  },

  //---------------------------------------------Methods END---------------------------------------------------------
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})